package com.wrox.locator;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

public class DataSourceLocator {
	private static DataSourceLocator dataSourceLocator = null;
	private static Map<String, DataSource> cache = null;

	private DataSourceLocator() {
		cache = new HashMap<String, DataSource>();

	}

	public static DataSourceLocator getInstance() {
		if (dataSourceLocator == null) {
			synchronized (DataSourceLocator.class) {

				if (dataSourceLocator == null) {
					dataSourceLocator = new DataSourceLocator();

				}

			}
		}
		return dataSourceLocator;
	}

	public DataSource getDataSources(String key) {
		if (cache.containsKey(key)) {
			return cache.get(key);

		} else {
			BasicDataSource basicDataSource = new BasicDataSource();
			basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
			basicDataSource.setUrl("jdbc:mysql://localhost:3306/employees");
			basicDataSource.setUsername("root");
			basicDataSource.setPassword("root");
			cache.put(key, basicDataSource);

		}
		return cache.get(key);
	}

}
